#!/bin/bash

SVCs=("spiker" "lib")

cd `dirname "$0"`

OUTPUT_DIR=${1:-build}
if [ ! -d "$OUTPUT_DIR" ]; then
    mkdir "$OUTPUT_DIR"
else
    read -p "build path:${OUTPUT_DIR} exists. continue[y / n]? " conti
    if [ "Y" != $(echo $conti | tr [a-z] [A-Z]) ]; then
        echo "$conti, exiting..."
        exit 0
    fi

    read -p "Would you likt to clear path:${OUTPUT_DIR} now[y / n]? " conti
    if [ "Y" == $(echo $conti | tr [a-z] [A-Z]) ]; then
        rm -rf "$OUTPUT_DIR"
        mkdir "$OUTPUT_DIR"
        echo "Re-created DIR:[ $OUTPUT_DIR ]"
    else
        echo "It would overried all exits files."
    fi
fi

mkdir "$OUTPUT_DIR/psvr"
mkdir "$OUTPUT_DIR/html"
mkdir "$OUTPUT_DIR/etc"
mkdir "$OUTPUT_DIR/logs"
mkdir "$OUTPUT_DIR/pid"

#copy html directory
cp -R ./html/* "$OUTPUT_DIR/html/"
echo "Directory [ html ] have been built successfully."

#copy etc directory
cp -R ./etc/*.default "$OUTPUT_DIR/etc/"
echo "Directory [ etc ] have been built successfully."

#build svcs
for svc in ${SVCs[@]};
do
    ./archiver $svc $OUTPUT_DIR/psvr
    echo "Service [ $svc ] have been built successfully."
done
